﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

using DotNetLib.Windows.Forms;

using ExtensibleEventViewer.Console.Adapters;
using ExtensibleEventViewer.Console.DomainModel;
using ExtensibleEventViewer.Console.EventArguments;
using ExtensibleEventViewer.Console.Forms;


namespace ExtensibleEventViewer.Console.Helpers
{
	internal sealed class EventLogMenuHelper
	{
		private readonly static EventLogMenuHelper _instance = new EventLogMenuHelper();
		private ContextMenuStrip _menu;

		public static EventLogMenuHelper Instance
		{
			get
			{
				return _instance;
			}
		}


		internal void BindContextMenu(ContextMenuStrip menu)
		{
			_menu = menu;

			_menu.Opening += MenuOpening;
		}

		private void MenuOpening(object sender, CancelEventArgs e)
		{
			ContainerListView listView = (ContainerListView)_menu.SourceControl;

			if (listView.SelectedItems.Count > 0)
			{
				if (listView.SelectedItems[0].Tag is EventLogAdapter)
				{
					EventLogAdapter adapter = (EventLogAdapter)listView.SelectedItems[0].Tag;

					_menu.Items.Clear();

					_menu.Items.Add(GetRefreshRecordsMenuItem(adapter));

					_menu.Items.Add(GetOpenRefreshIntervalSettingsWindowMenuItem(adapter));

					_menu.Items.Add(GetSeparator());

					_menu.Items.Add(GetClearRecordsMenuItem(adapter));
					
					e.Cancel = false;
				}
				else
				{
					e.Cancel = true;
				}
			}
			else
			{
				e.Cancel = true;
			}
		}

		private static ToolStripMenuItem GetRefreshRecordsMenuItem(EventLogAdapter adapter)
		{
			EventHandler refreshHandler = (obj, args) => adapter.RefreshRecords();

			return new ToolStripMenuItem("Refresh", null, refreshHandler);
		}

		private ToolStripMenuItem GetOpenRefreshIntervalSettingsWindowMenuItem(EventLogAdapter adapter)
		{
			EventHandler openRefreshIntervalSettingsWindowHandler = (obj, args) => OpenAdapterSettingsWindow(adapter);
			
			return new ToolStripMenuItem("Refresh Settings", null, openRefreshIntervalSettingsWindowHandler);
		}

		private static ToolStripMenuItem GetClearRecordsMenuItem(EventLogAdapter adapter)
		{
			EventHandler clearRecordsHandler = (obj, args) =>
			                                   	{
													if (adapter.DataItem.Clear())
			                                   		{
														adapter.ListViewItem.Items.Clear();
			                                   		}
			                                   	};
			
			return  new ToolStripMenuItem("Clear", null, clearRecordsHandler);
		}

		private static ToolStripSeparator GetSeparator()
		{
			return new ToolStripSeparator();
		}

		private void OpenAdapterSettingsWindow(EventLogAdapter adapter)
		{
			using (EventLogAdapterSettingsWnd settingsWnd = new EventLogAdapterSettingsWnd())
			{
				settingsWnd.EnableRefresh = adapter.Settings.EnableRefresh;
				settingsWnd.RefreshInterval = adapter.Settings.RefreshInterval;
				settingsWnd.UseDefaultRefreshInterval = adapter.Settings.UseDefaultRefreshInterval;

				EventHandler<SaveEventLogAdapterSettingsArgs> saveSettings = (sender, args) =>
				                                                             	{
				                                                             		adapter.Settings.UseDefaultRefreshInterval =
				                                                             			args.UseDefaultRefreshInterval;
				                                                             		adapter.Settings.RefreshInterval =
				                                                             			args.RefreshInterval;
				                                                             		adapter.Settings.EnableRefresh = args.EnableRefresh;

				                                                             		SaveAdapterSettings(adapter.ListViewItem.ListView);
				                                                             	};

				settingsWnd.SaveSettings += saveSettings;
				
				settingsWnd.ShowDialog(_menu.FindForm());
			}
		}

		private void SaveAdapterSettings(ContainerListView listView)
		{
			List<EventLogAdapterSettingsData> settings = new List<EventLogAdapterSettingsData>();

			foreach (ContainerListViewItem item in listView.Items)
			{
				if (item.Tag is EventLogAdapter)
				{
					EventLogAdapter adapter = (EventLogAdapter) item.Tag;

					settings.Add(new EventLogAdapterSettingsData
					             	{
					             		EventLogEntityID = adapter.DataItem.GetHashCode(),
					             		EnableRefresh = adapter.Settings.EnableRefresh,
					             		RefreshIntervalTicks = adapter.Settings.RefreshInterval.Ticks,
					             		UseDefaultRefreshInterval =
					             			adapter.Settings.UseDefaultRefreshInterval
					             	});
				}
			}

			AppSettings.Instance.AdapterSettings = settings.ToArray();
		}
	}
}